import sys, os
try:
        import pygtk
        pygtk.require("2.0")
except:
        pass
try:
        import gtk
        import gtk.glade
except:
        sys.exit(1)
from simpledcpp.gui.base import gtk_lock_required
class Pager:
	def __init__(self, parent):
		self.parent = parent
		self.pages = []
		self._widget = self.parent.wtree.get_widget("Pager")
		self._widget.set_scrollable(True)
		self._widget.connect('switch-page', self._onchange)
	
	@gtk_lock_required
	def _onchange(self, notebook, page, page_num, *args, **kwargs):
		now_widget = notebook.get_nth_page(page_num)
		for i in self.pages:
			if i._widget == now_widget:
#				print "Setting angle to default."
				i._label.set_angle(0)
				i.activate()
		
		
	@gtk_lock_required
	def add(self, Page):
		self.pages.append(Page)
		titlebox = gtk.HBox()
		label = gtk.Label(Page.name)
		button = gtk.Button()
                button.set_relief(gtk.RELIEF_NONE)
                button.set_focus_on_click(False)
                icon = gtk.image_new_from_stock(gtk.STOCK_CLOSE,
                        gtk.ICON_SIZE_MENU)
                icon.show()
                button.add(icon)
		label.show()
		Page._label = label
		button.show()
		titlebox.pack_start(label)
		titlebox.pack_start(button)
		titlebox.show()
		pid = self._widget.append_page(Page._widget, titlebox)
		self._widget.set_current_page(pid)
		button.connect('clicked', self._ClosePage, Page)
		
	@gtk_lock_required
	def _ClosePage(self, *args, **kwargs):
		count = self._widget.get_n_pages()
		for i in range(0, count):
			if self._widget.get_nth_page(i) == args[1]._widget:
				self._widget.remove_page(i)
				self.pages.remove(args[1])
				args[1].Close()
				return
	
